import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:gsy_flutter_app/common/config/Config.dart';
import 'package:gsy_flutter_app/common/dao/UserDao.dart';
import 'package:gsy_flutter_app/common/local/LocalStorage.dart';
import 'package:gsy_flutter_app/common/net/ResultData.dart';
import 'package:gsy_flutter_app/common/style/GSYStyle.dart';
import 'package:gsy_flutter_app/widget/GSYFlexButton.dart';
import 'package:gsy_flutter_app/widget/GSYinputWidget.dart';

/// 作者: Tao
/// 日期: 2024/1/17
/// 时间: 9:45
/// 描述: 登录界面

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  LoginPageState createState() => LoginPageState();
}

class LoginPageState extends State<LoginPage> {
  var userName = "";
  var passWord = "";
  final TextEditingController userController = TextEditingController();
  final TextEditingController pwdController = TextEditingController();

  @override
  void initState() {
    super.initState();
    initParams();
  }

  initParams() async {
    var un = await LocalStorage.get(Config.USER_NAME_KEY);
    if (un != null) {
      userName = un;
    }
    var pwd = await LocalStorage.get(Config.PW_KEY);
    if (pwd != null) {
      passWord = pwd;
    }
    userController.value = TextEditingValue(text: userName);
    pwdController.value = TextEditingValue(text: passWord);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color(GSYColors.primaryValue),
      child: Center(
        child: Card(
          //阴影高度
          elevation: 5.0,
          //形状(感觉加了没啥变化，先不管)
          // shape: const RoundedRectangleBorder(
          //     borderRadius: BorderRadius.all(Radius.circular(10))),
          color: const Color(GSYColors.cardWhite),
          //背景颜色
          margin: const EdgeInsets.all(20),
          child: Padding(
            padding:
                const EdgeInsets.only(left: 30, top: 60, right: 30, bottom: 80),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                const Image(
                    image: AssetImage('static/images/logo.png'),
                    width: 80,
                    height: 80),
                const Padding(padding: EdgeInsets.all(10)),
                GSYInputWidget(
                  hintText: GSYStrings.login_username_hint_text,
                  iconData: Icons.account_box,
                  onChanged: (value) {
                    if (Config.DEBUG) {
                      print('账号为:$value');
                    }
                    userName = value;
                  },
                  controller: userController,
                ),
                const Padding(padding: EdgeInsets.all(10)),
                GSYInputWidget(
                    obscureText: true,
                    hintText: GSYStrings.login_password_hint_text,
                    iconData: Icons.security,
                    onChanged: (value) {
                      if (Config.DEBUG) {
                        print('密码为:$value');
                      }
                      passWord = value;
                    },
                    controller: pwdController),
                const Padding(padding: EdgeInsets.all(30)),
                GSYFlexButton(
                    text: GSYStrings.login_text,
                    onPress: () {
                      if (userName.isEmpty || passWord.isEmpty) {
                        return;
                      }
                      UserDao.login(userName, passWord, (ResultData data) {
                        if (Config.DEBUG) {
                          print('登录结果为：$data');
                        }
                        if (data.result) {
                          Fluttertoast.showToast(msg: GSYStrings.login_success);
                        }
                      });
                    })
              ],
            ),
          ),
        ),
      ),
    );
  }
}
